package com.nine.pesys.util.interceptor;

import com.nine.pesys.model.entity.User;
import com.nine.pesys.model.service.UserService;
import com.nine.pesys.util.common.CommomUtils;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.HttpCookie;
import java.util.List;

/**
 * 看用户是否之前选择自动登录
 * Created by L先森 on 2014/12/26.
 */
@SuppressWarnings("all")
public class AutoLoginItc extends AbstractInterceptor {
    @Autowired
    private UserService userService;
    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        HttpServletRequest request  = ServletActionContext.getRequest();
        String[] loginInfo = CommomUtils.getCookie(request,"loginInfo");
        System.out.println("============================'");
        if (loginInfo!=null){
            for (int i = 0 ; i <loginInfo.length;i++){
                System.out.println(loginInfo[i]+"============loginInfo==============='");
            }
        }
        if(loginInfo!=null&&loginInfo.length>1){
            User user = new User();
            user.setStuNumber(loginInfo[0]);
            user.setPassword(loginInfo[1]);
            user = this.userService.getByCondition(user).get(0);
            HttpSession session = ServletActionContext.getRequest().getSession();
            session.setAttribute("user",user);
            return actionInvocation.invoke();
        }
        return actionInvocation.invoke();
    }
}
